package com.he.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.LogComment;
import com.he.pojo.LogCommentExample;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.pojo.UserExample.Criteria;
import com.he.service.logComment.LogCommentServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.userLog.UserLogServiceI;
import com.he.pojo.UserLog;
import com.he.pojo.UserLogExample;

@Controller
@RequestMapping("Log")
public class LogControll {
	@Autowired
	UserServiceI userServiceI;
	@Autowired
	UserLogServiceI userLogserviceI;
	@Autowired
    LogCommentServiceI logcommentserviceI;
	@RequestMapping("/show")
	@ResponseBody
	public String  partlogShow(HttpServletRequest request) {
		int resign=0;
		User user=new User();
		user.setWithPage(0);
		HttpSession session = request.getSession();
		String useraccount=(String) session.getAttribute("useraccount");
		 UserExample uex=new UserExample();
	     Criteria uc = uex.createCriteria();
		uc.andUserAccountEqualTo(useraccount);
		List<User> list=userServiceI.getUsers(uex, user) ;
		int userid=0;
		for (User user2 : list) {
			userid=user2.getId();
		}
        UserLogExample ule=new UserLogExample();
        com.he.pojo.UserLogExample.Criteria lc = ule.createCriteria();
        lc.andUserIdEqualTo(userid);
		UserLog userLog=new UserLog();
		userLog.setPageSize(5);
		List<UserLog> llist= userLogserviceI.getUserLogs(ule, userLog);

		List<UserLog> partlist = null;
		//查询到的5个log
	    for (UserLog userLog2 : llist) {
		 if(resign<5){
			partlist.add(userLog2);
			resign++;
	    	}
	  
	      }
	request.setAttribute("clist", partlist);
	//详细评论的查询
	LogCommentExample lce=new LogCommentExample();
	com.he.pojo.LogCommentExample.Criteria clce = lce.createCriteria();
	LogComment logcomment=new LogComment();
	clce.andUserIdEqualTo(userid);
	List<LogComment> lclist=logcommentserviceI.getLogComments(lce,logcomment);
	//
		return "profile";
	}
	@RequestMapping("/showcomment")
	@ResponseBody
	public List<LogComment>  logcommentShow(HttpServletRequest request) {
		User user=new User();
		user.setWithPage(0);
		HttpSession session = request.getSession();
		String useraccount=(String) session.getAttribute("useraccount");
		 UserExample uex=new UserExample();
	     Criteria uc = uex.createCriteria();
		uc.andUserAccountEqualTo(useraccount);
		List<User> list=userServiceI.getUsers(uex, user) ;
		int userid=0;
		for (User user2 : list) {
			userid=user2.getId();
		}

		LogCommentExample lce=new LogCommentExample();
		com.he.pojo.LogCommentExample.Criteria clce = lce.createCriteria();
		LogComment logcomment=new LogComment();
		clce.andUserIdEqualTo(userid);
		List<LogComment> lclist=logcommentserviceI.getLogComments(lce,logcomment);
		return lclist;
		
	}
	
}
